package yxy.game.pm2.command.worldboss;

import yxy.cherry.data.bean.menghuoboss;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.WorldBossMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.worldboss.WorldBossHandle;
import yxy.game.pm2.module.worldboss.WorldBossPlayer;

/** 世界BOSS 世界BOSS购买挑战次数 */
@MessageType(proto = Command.WorldBossPurchase, classtype = WorldBossMessage.WorldBossPurchaseRequest.class)
public class CMD_WorldBossPurchase extends Command<WorldBossMessage.WorldBossPurchaseRequest> {

	@Override
	public void execute(MessageSource messageSource, WorldBossMessage.WorldBossPurchaseRequest request) {
		String playerCode = messageSource.getTag();

		WorldBossMessage.WorldBossPurchaseResponse.Builder response = WorldBossMessage.WorldBossPurchaseResponse.newBuilder();

		// 世界等级判断
		WorldBossHandle bossHandle = new WorldBossHandle();
		AreaBean areaBean = AreaModule.area(playerCode);
		Result result = bossHandle.checkWorldLevel(areaBean);
		if (!result.equals(Result.Success)) {
			send(messageSource, response, result);
			return;
		}

		WorldBossPlayer worldBossPlayer = WorldBossPlayer.get(playerCode);
		try {
			if (worldBossPlayer.tryLock()) {
				if (worldBossPlayer.remainBuyTimes() <= 0) {
					send(messageSource, response, Result.ParamError);// 没有次数了 你的挑战次数用完了
					return;
				}
				// 有购买次数 直接花钱挑战
				menghuoboss data = bossHandle.data();
				int costNumber = data.getSpend();
				Currency cost = Currency.create(CurrencyType.CT_元宝, 0, costNumber);
				try {
					new CurrencyHandle().cost(playerCode, CCReason.七擒孟获, cost);
					worldBossPlayer.setBuytimes(worldBossPlayer.getBuytimes() + 1);
					worldBossPlayer.setTimes(worldBossPlayer.getTimes() - 1);
					worldBossPlayer.set();
				} catch (CurrencyNotEnoughException e) {
					send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
					return;
				}
				
				response.setTimes(worldBossPlayer.remainTimes());
				response.setBuytimes(worldBossPlayer.remainBuyTimes());
				
				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			send(messageSource, response, Result.DataError);
			e.printStackTrace();
		} finally {
			worldBossPlayer.unlock();
		}
	}

	private void send(MessageSource messageSource, WorldBossMessage.WorldBossPurchaseResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}